<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
use Magento\Framework\App\Action\Action;

// @codingStandardsIgnoreFile
?>
<?php
/**
 * Product list template
 *
 * @var $block \Smartwave\Filterproducts\Block\
 */
?>
<?php
$_productCollection = $block->getLoadedProductCollection();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');

$_portohelper = $this->helper('Smartwave\Porto\Helper\Data');
$review_model = $_portohelper->getModel('\Magento\Review\Model\Review');

$_category_config = $_portohelper->getConfig('porto_settings/category');
$_category_grid_config = $_portohelper->getConfig('porto_settings/category_grid');

$aspect_ratio = $this->getData("aspect_ratio");
if($aspect_ratio == null) {
    $aspect_ratio = $_category_config['aspect_ratio'];
}
$image_width = $this->getData("image_width");
if(!$image_width) {
    $image_width = ($_category_config['ratio_width'])?$_category_config['ratio_width']:300;
}
$image_height = $this->getData("image_height");
if(!$image_height) {
    $image_height = ($_category_config['ratio_height'])?$_category_config['ratio_height']:300;  
}
?>
<?php if (!$_productCollection->count()): ?>
    <div class="message info empty"><div><?php echo __('We can\'t find products matching the selection.') ?></div></div>
<?php else: ?>
    <?php
    $viewMode = 'grid';
    $image = 'category_page_grid';
    $hover_image = 'category_page_grid-hover';
    $showDescription = false;
    $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
    ?>
    <div class="products wrapper <?php echo $viewMode; ?> side-list products-<?php echo $viewMode; ?>">
        <?php $iterator = 0; ?>
        <div class="filterproducts products list items product-items owl-carousel">
            <?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
            <?php foreach ($_productCollection as $_product): ?>
            <?php if(($iterator % 3) == 0): ?>
            <div class="item">
            <?php endif; ?>
            <?php $iterator ++; ?>
                <div class="product product-item">
                <div class="product-item-info" data-container="product-grid">
                    <?php // Product Image ?>
                    <a href="<?php echo $_product->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1">
                        <?php
                            if($aspect_ratio)
                                $productImage = $_imagehelper->init($_product, $image)->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize($image_width);
                            else
                                $productImage = $_imagehelper->init($_product, $image)->resize($image_width, $image_height);
                            $productImageUrl = $productImage->getUrl();
                        ?>
                            <img class="product-image-photo default_image" src="<?php echo $productImageUrl; ?>" alt="<?php echo $productImage->getLabel(); ?>"/>
                        <?php if($_category_config['alternative_image']): ?>
                        <?php
                            if($aspect_ratio)
                                $productHoverImage = $_imagehelper->init($_product, $hover_image)->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize($image_width);
                            else
                                $productHoverImage = $_imagehelper->init($_product, $hover_image)->resize($image_width, $image_height);
                            $productHoverImageUrl = $productHoverImage->getUrl();
                        ?>
                            <?php if($productImageUrl != str_replace("/thumbnail/","/small_image/",$productHoverImageUrl)): ?>
                            <img class="product-image-photo hover_image" src="<?php echo $productHoverImageUrl; ?>" alt="<?php echo $productHoverImage->getLabel(); ?>"/>
                            <?php endif; ?>
                        <?php endif; ?>
                    </a>
                    <div class="product details product-item-details">
                        <?php
                            $_productNameStripped = $block->stripTags($_product->getName(), null, true);
                        ?>
                        <strong class="product name product-item-name">
                            <a class="product-item-link"
                               href="<?php echo $_product->getProductUrl() ?>">
                                <?php echo $_helper->productAttribute($_product, $_product->getName(), 'name'); ?>
                            </a>
                        </strong>
                        <?php if($_category_config['rating_star']): ?>
                        <?php
                            $review_model->getEntitySummary($_product);
                            $review_html = $block->getReviewsSummaryHtml($_product, $templateType);
                        ?>
                        <?php if($review_html): ?>
                            <?php echo $block->getReviewsSummaryHtml($_product, $templateType); ?>
                        <?php else: ?>
                        <div class="product-reviews-summary short">
                            <div class="rating-summary">
                                <span class="label"><span>Rating:</span></span>
                                <div class="rating-result" title="0%">
                                    <span style="width:0"><span>0%</span></span>
                                </div>
                            </div>
                        </div>
                        <?php endif; ?>
                        <?php endif; ?>
                        <?php if ($showDescription):?>
                            <div class="product description product-item-description">
                                <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>"
                                   class="action more"><?php echo __('Learn More') ?></a>
                            </div>
                        <?php endif; ?>
                        <?php if($_category_config['product_price']): ?>
                        <?php echo $block->getProductPrice($_product) ?>
                        <?php endif; ?>
                        <?php echo $block->getProductDetailsHtml($_product); ?>
                    </div>
                </div>
                </div>
                <?php if(($iterator % 3) == 0 || $iterator == $_productCollection->count()): ?>
                </div>
                <?php endif; ?>
            <?php endforeach; ?>
        </div>
    </div>
    <?php if (!$block->isRedirectToCartEnabled()) : ?>
        <script type="text/x-magento-init">
        {
            "[data-role=tocart-form], .form.map.checkout": {
                "catalogAddToCart": {}
            }
        }
        </script>
    <?php endif; ?>
<?php endif; ?>